package fr.norsys.mapper.console.web;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ltd.getahead.dwr.ExecutionContext;
import fr.norsys.mapper.console.exception.ApplicationException;
import fr.norsys.mapper.console.exception.ConnectionException;
import fr.norsys.mapper.console.exception.MappingException;
import fr.norsys.mapper.console.exception.PropertyException;
import fr.norsys.mapper.console.model.Application;
import fr.norsys.mapper.console.model.Connection;
import fr.norsys.mapper.console.model.ConsoleCst;
import fr.norsys.mapper.console.service.ApplicationService;
import fr.norsys.mapper.console.service.ConnectionService;
import fr.norsys.mapper.console.service.MappingService;
import fr.norsys.mapper.console.service.PropertyService;
import fr.norsys.mapper.console.utils.BeanUtils;

public class ConfigSaverDWR {
	private final static Log log = LogFactory.getLog(ConfigSaverDWR.class);
	private MappingService mappingService;
	private ApplicationService applicationService;
	private ConnectionService connectionService;
	private PropertyService propertyService;
	
	public PropertyService getPropertyService() {
		return propertyService;
	}
	public void setPropertyService(PropertyService propertyService) {
		this.propertyService = propertyService;
	}
	public ConnectionService getConnectionService() {
		return connectionService;
	}
	public void setConnectionService(ConnectionService connectionService) {
		this.connectionService = connectionService;
	}
	public ApplicationService getApplicationService() {
		return applicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}
	public MappingService getMappingService() {
		return mappingService;
	}
	public void setMappingService(MappingService mappingService) {
		this.mappingService = mappingService;
	}
	public boolean saveApplicationConfig(String id)  throws Exception {
		Application application = null;
			application = applicationService.get(id,(Collection)ExecutionContext.get().getSession().getAttribute(ConsoleCst.APPLICATIONS_SESSION_BEAN));
		try {
			mappingService.saveMapping(application,applicationService.getAppliDirPath()+application.getName()+".xml");
		} catch(MappingException e) {
			log.error("error occured when saving application into a file:"+e);
			return false;
		}
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.IS_APPLICATION_MODIFIED,ConsoleCst.APPLICATION_NOT_MODIFIED);
		return true;
	}
	public boolean saveConnectionConfig(String id)  throws Exception {
		Connection connection = null;
			connection = connectionService.get(id,(Collection)ExecutionContext.get().getSession().getAttribute(ConsoleCst.CONNECTIONS_SESSION_BEAN));
		Map map = BeanUtils.toMap(connection);
		map.remove("class");
		map.remove("id");
		try {
			propertyService.setProperty(connectionService.getConnectionsDirPath()+connection.getName()+".properties",map);
		} catch(PropertyException e) {
			log.error(e);
			return false;
		}
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.IS_CONNECTION_MODIFIED,
				ConsoleCst.CONNECTION_NOT_MODIFIED);
		return true;
	}

	
	public String saveApplicationConfigAndLoad(String id, String href)  throws Exception {
		Application application = null;
			application = applicationService.get(id,(Collection)ExecutionContext.get().getSession().getAttribute(ConsoleCst.APPLICATIONS_SESSION_BEAN));
		try {
			mappingService.saveMapping(application,applicationService.getAppliDirPath()+application.getName()+".xml");
		} catch(MappingException e) {
			log.error("error occured when saving application into a file:"+e);
			return null;
		}
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.IS_APPLICATION_MODIFIED,ConsoleCst.APPLICATION_NOT_MODIFIED);
		return href;
	}
	public String saveConnectionConfigAndLoad(String id, String href)  throws Exception {
		Connection connection = null;
			connection = connectionService.get(id,(Collection)ExecutionContext.get().getSession().getAttribute(ConsoleCst.CONNECTIONS_SESSION_BEAN));
		Map map = BeanUtils.toMap(connection);
		map.remove("class");
		map.remove("id");
		try {
			propertyService.setProperty(connectionService.getConnectionsDirPath()+connection.getName()+".properties",map);
		} catch(PropertyException e) {
			log.error(e);
			return null;
		}
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.IS_CONNECTION_MODIFIED,ConsoleCst.CONNECTION_NOT_MODIFIED);
		return href;
	}
}
